package com.yoso.shop.application.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;
import com.yoso.base.service.BaseService;
import com.yoso.shop.application.service.IAdminService;
import com.yoso.shop.domain.user.AdminEntity;
import com.yoso.shop.domain.user.AdminEntityExample;
import com.yoso.shop.infrastructure.dao.user.AdminEntityMapper;
import com.yoso.shop.interfaces.vo.user.AdminVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * @author shizejin
 * @date 2021/11/9
 * @description:
 */
@Service
public class AdminServiceImpl extends BaseService<AdminEntity, AdminVo, AdminEntityExample> implements IAdminService {
    @Autowired
    private AdminEntityMapper adminEntityMapper;

    @Override
    public AdminEntity V2E(AdminVo data) {
        return JSON.parseObject(JSON.toJSONString(data), AdminEntity.class);
    }

    @Override
    public AdminVo E2V(AdminEntity data) {
        return JSON.parseObject(JSON.toJSONString(data), AdminVo.class);
    }

    @Override
    public AdminEntityExample getEntityExample(Map<String, String> paramMap) {
        return new AdminEntityExample();
    }

    /**
     * 管理员登录
     */
    @Override
    public AdminVo login(AdminVo admin) {
        AdminEntityExample example = new AdminEntityExample();
        example.createCriteria()
                .andPasswordEqualTo(SecureUtil.md5(admin.getPassword()))
                .andUsernameEqualTo(admin.getUsername());
        List<AdminEntity> result = adminEntityMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(result)) {
            return null;
        }
        return E2V(result.get(0));
    }

}
